// subpages/GeneralreservationOrderDetail/GeneralreservationOrderDetail.js
//获取应用实例
var app = getApp()
Page({
  data: {
    codeShow: false,
    tkMoney:'',
    tkReason: '',
    tkContact: ''
  },
  onLoad: function (e) {
    var that = this;
    if(e&&e.tid){
      that.setData({
        tid:e.tid
      })
    };
    if (!app.globalData.plumSession) {
      app.wechatSq(that);
    } else {
      app.setNavtitle('预约详情');
      that.requestDetail();
    }
  },
  onShow:function(){
    var that = this;

  },
  requestDetail: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    });
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        tid: that.data.tid,
        map: 'applet_appointment_order_detail'
      },
      success: function (res) {
        console.log(res.data.data)
        if (res.data.ec == 200) {
          var dataInfo = res.data.data;
          that.setData({
            orderDetail: dataInfo
          })
          console.log(dataInfo);
        } else {

        }
      },
      fail: function () {
        console.log("fail")
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    })
  },
  //核销二维码显示
  codeModelShow: function () {
    var that = this;
    that.setData({
      codeShow: true
    })
  },
  //核销二维码弹窗关闭
  closeCodeModel: function () {
    var that = this;
    that.setData({
      codeShow: false
    })
  },
  makeCall: function () {
    var phone = app.globalData.telphone;
    wx.showModal({
      title: '',
      content: '拨打商家电话' + phone + '?',
      success: function (res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: phone,
            success: function () {
              console.log("拨打电话成功！")
            },
            fail: function () {
              console.log("拨打电话失败！")

            }
          })
        }
      }
    })
  },
  applyTuikuan: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.tid;
    var total = e.currentTarget.dataset.total;
    that.setData({
      tkMoney: total,
      tkReason: "",
      tkContact: "",
      isShowModal: true,
      tkOrderid: orderId
    })
  },
  hideModal: function () {
    this.setData({
      isShowModal: false
    })
  },
  tkMoneyChange: function (e) {
    this.setData({
      tkMoney: e.detail.value
    })
  },
  tkReasonChange: function (e) {
    this.setData({
      tkReason: e.detail.value
    })
  },
  tkContactChange: function (e) {
    this.setData({
      tkContact: e.detail.value
    })
  },
  submitTkapply: function () {
    var that = this;
    app.getSubId(that, 'applet_order_refund', 'toSubmitTkapply');
  },
  toSubmitTkapply: function () {
    var that = this;
    console.log('123');
    var data = {};
    data.map = 'applet_order_refund';
    // data.suid = app.globalData.suid,
    data.tid = that.data.tkOrderid;
    data.money = that.data.tkMoney;
    data.reason = that.data.tkReason;
    data.contact = that.data.tkContact;

    if (data.money == "") {
      app.errorTip(that, "请输入退款金额", 2000);
      return;
    }
    // if (parseFloat(data.money) > that.data.orderDetail.total) {
    //   app.errorTip(that, "退款金额不能大于商品总金额", 2000);
    //   return;
    // }
    if (data.reason == "") {
      app.errorTip(that, "请输入退款原因", 2000);
      return;
    }
    if (data.contact == "") {
      app.errorTip(that, "请输入您的联系方式", 2000);
      return;
    }
    console.log(data);
    //发起请求，获取列表列表
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 10000
    });
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data);
          app.errorTip(that, res.data.data.msg, 2000);
          setTimeout(function () {
            that.onLoad();
          }, 1000);
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        that.setData({
          isShowModal: false
        })
        wx.hideToast();
      }
    });
  },
  cancelTk: function (e) {
    var that = this;
    var tid = e.target.dataset.tid;
    wx.showModal({
      title: '',
      cancelText: '再考虑下',
      confirmText: '确认',
      content: '确认撤销维权吗？',
      confirmColor: '#1AAD16',
      success: function (res) {
        if (res.confirm) {
          //发起请求，获取列表列表
          wx.showToast({
            title: '加载中',
            icon: 'loading',
            mask: true,
            duration: 10000
          });
          wx.request({
            url: app.globalData.requestUrl,
            data: {
              map: 'applet_order_refund_cancel',
              // suid: app.globalData.suid,
              tid: tid
            },
            success: function (res) {
              if (res.data.ec == 200) {
                console.log(res.data.data);
                app.errorTip(that, res.data.data.msg, 2000);
                setTimeout(function () {
                  that.onLoad();
                }, 1000);
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.em,
                  showCancel: false
                });
              }
            },
            complete: function () {
              wx.hideToast();
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  seeWeiquan: function (e) {
    var that = this;
    var orderId = e.target.dataset.tid;
    console.log(orderId);
    wx.navigateTo({
      url: '/subpages/feedbackRecord/feedbackRecord?orderid=' + orderId
    })
  },
  //取消订单
  cancelOrder: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.tid;
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_cancel_order',
        tid: id,
        plum_session_applet: app.globalData.plumSession

      },
      success: function (res) {

        console.log(res)
        if (res.data.ec == 200) {
          that.onLoad()
          that.showTost(res.data.data.msg)
        } else {
          that.showTost(res.data.em)
        }
      },
      fail: function () {

      }
    })
  },
  //付款点击
  paymentClick: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.tid;
    wx.request({
      url: app.globalData.requestUrl,

      // url: app.getRequestUrl("/applet.php?map=applet_order_pay"),
      data: {
        map: 'applet_order_pay',
        tid: id,
        money: that.data.orderDetail.total,
        plum_session_applet: app.globalData.plumSession

      },
      success: function (res) {
        console.log(res.data)
        if (res.data.ec == 200) {
          wx.requestPayment({
            'appId': res.data.data.appId,
            'timeStamp': res.data.data.timeStamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            'success': function (res) {
              console.log(res);
              console.log("支付成功");
              that.showTost("支付成功")
              that.onLoad();
            },
            'fail': function (res) {
              that.showTost("支付失败")

            }
          });
        } else {
          wx.hideLoading()
          that.showTost(res.data.em);


        }
      },
      fail: function () {
        wx.hideLoading()

      }
    })
  },
  showTost: function (toast) {
    wx.showToast({
      title: toast,
      image: "/images/hud_info.png",
      // icon: "loading",
      duration: 1000,
      mask: true
    })
  },
  onPullDownRefresh:function(){
    var that = this;
    that.onLoad();
  },
})